package cn.wsn2018.tools.service;

import cn.wsn2018.tools.common.CommonValue;
import cn.wsn2018.tools.mapper.FieldsMapper;
import cn.wsn2018.tools.mapper.UserMapper;
import cn.wsn2018.tools.model.FieldDesign;
import cn.wsn2018.tools.model.Fields;
import cn.wsn2018.tools.model.FieldsDesignModel;
import cn.wsn2018.tools.util.JsonUtil;
import net.sf.json.JSONArray;
import org.apache.log4j.Logger;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用于处理用户协议字段查询，协议字段设计的业务逻辑类
 */
@MapperScan("cn.wsn2018.tools.mapper")
@Service
public class FieldsService {
    @Resource
    private FieldsMapper fieldsMapper;
    @Resource
    private UserMapper userMapper;
    private Logger logger=Logger.getLogger(FieldsService.class);
    /**
     * 处理根据协议名查询所有字段的业务逻辑类
     * @param protocolName 用户提供的协议名
     * @return
     */
    public String dealFieldsQuery(String protocolName){
        List<Fields> list=fieldsMapper.listByProtocolName(protocolName);
        if(list.isEmpty()){
            return JsonUtil.failedJson(CommonValue.JSON_QUERY_FIELDS_FAILED);
        }
        else{
            JSONArray arr=JSONArray.fromObject(list);
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS,null,arr);
        }
    }

    /**
     * 处理安全推荐模板查询的业务逻辑类
     * @return
     */
    public String dealModelsQuery(){
        List<FieldsDesignModel> list=fieldsMapper.listModels();
        if(list.isEmpty()){
            return JsonUtil.failedJson(CommonValue.JSON_QUERY_FAILED);
        }
        else{
            JSONArray arr=JSONArray.fromObject(list);
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS,null,arr);
        }
    }

    /**
     * 批量录入用户设计的字段信息
     * @param username
     * @param protocol
     * @param list
     * @return
     */
    public String dealSaveFieldDesigns(String username,String protocol,List<FieldDesign> list){
        //首先根据用户名和协议名获取id
        Integer userId=userMapper.getIdByUserName(username);
        Integer protocolId=fieldsMapper.getProtocolId(protocol);
        logger.debug(username+":"+userId+":"+protocolId+":"+protocol);
        //将该用户对此协议曾经做的改动置空
        List<Integer> fieldDesignIds=fieldsMapper.listFieldDesignIds(userId,protocolId);
        for(int i=0;i<fieldDesignIds.size();i++)
            fieldsMapper.remove(fieldDesignIds.get(i));
        //将用户所做的协议字段设计信息逐条插入到数据库对应表中
        for(int i=0;i<list.size();i++){
            FieldDesign fieldDesign=list.get(i);
            fieldsMapper.saveFieldDesign(fieldDesign);
            Integer fieldDesignId=fieldDesign.getId();
            String type=fieldDesign.getType();
            //将字段设计信息插入到关联表中
            fieldsMapper.saveReFieldDesign(userId,protocolId,fieldDesignId,type);
            fieldsMapper.saveReFieldValue(fieldDesignId,"0");
        }
        return JsonUtil.successJson(CommonValue.JSON_INSERT_SUCCESS,null,null);
    }

    /**
     * 预览用户编辑的协议，头部部分为待改进协议的字段
     * @param username
     * @param protocol
     * @return
     */
    public String dealReviewProtocolDesign(String username,String protocol){
        List<Fields> protocolFields=fieldsMapper.listByProtocolName(protocol);
        if(protocolFields.isEmpty()){
            return JsonUtil.failedJson(CommonValue.JSON_QUERY_FIELDS_FAILED);
        }
        else {
            List<Fields> userDesignFields=fieldsMapper.listFieldsByUserAndProtocol(username,protocol);
            logger.debug(userDesignFields.toString());
            protocolFields.addAll(userDesignFields);
            JSONArray arr=JSONArray.fromObject(protocolFields);
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS,null,arr);
        }
    }
}
